package com.rcadmin.busi.web.dao;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.rcadmin.busi.web.contant.GroupType;
import com.rcadmin.busi.web.ddl.WebTemplateGroupkey;
import com.rcadmin.common.contant.IsEff;
import com.rcadmin.core.jpa.BaseDao;
import com.rcadmin.core.jpa.DaoException;
import com.rcadmin.util.SqlUtil;

/**
 * 分类DAO
 * 
 * @author tanson lam
 * @create 2016年10月18日
 */
@Component
public class GroupKeyDao {

	@Resource
	private BaseDao baseDao;

	/**
	 * 查找单个分类信息
	 * 
	 * @param userId
	 * @param groupKeyId
	 * @return
	 */
	public WebTemplateGroupkey findOne(Integer userId, Integer groupKeyId) {
		StringBuilder query = new StringBuilder();
		query.append(" and id = ").append(groupKeyId);
		query.append(" and user_id = ").append(userId);
		return baseDao.findOne(query.toString(), WebTemplateGroupkey.class);
	}

	/**
	 * 查找改用户下所有分组KEY
	 * 
	 * @param userId
	 * @param templatePackageId
	 * @return
	 */
	public List<WebTemplateGroupkey> findEffGroupKeys(Integer userId,
			Integer templatePackageId) {
		StringBuilder query = new StringBuilder();
		query.append(" and user_id = ").append(userId);
		query.append(" and iseff = '").append(IsEff.EFFECTIVE).append("'");
		query.append(" and template_package_id=").append(templatePackageId);
		return baseDao.find(query.toString(), WebTemplateGroupkey.class);
	}

	/**
	 * 查询所有生效分类信息
	 * 
	 * @param templatePackageId
	 * @return
	 */
	public List<WebTemplateGroupkey> findAllEff() {
		return baseDao.find(" iseff=" + IsEff.EFFECTIVE + " order by id",
				WebTemplateGroupkey.class);
	}

	/**
	 * 是否有相同的分类key
	 * 
	 * @param groupKeyId
	 * @param groupKey
	 * @return
	 */
	public Boolean isDuplicateKey(Integer userId, Integer groupKeyId,
			String groupKey) {
		String sql = " user_id=" + userId + " and group_key ='" + groupKey
				+ "' ";
		if (groupKeyId != null)
			sql += " and id !=" + groupKeyId;
		List<WebTemplateGroupkey> list = baseDao.find(sql,
				WebTemplateGroupkey.class);
		if (list != null && !list.isEmpty()) {
			return true;
		}
		return false;
	}

	/**
	 * 根据主题包ID获取分类信息
	 * 
	 * @param templatePackageId
	 * @return
	 */
	public List<WebTemplateGroupkey> findGroupKeyByTemplatePackageId(
			Integer templatePackageId) {
		if (templatePackageId == null)
			return Lists.newArrayList();
		return baseDao.find("template_package_id=" + templatePackageId
				+ " and iseff=" + IsEff.EFFECTIVE + " order by id",
				WebTemplateGroupkey.class);
	}

	/**
	 * 根据主题包ID和多个分类KEY获取分类信息
	 * 
	 * @param templatePackageId
	 * @param groupKeys
	 * @return
	 */
	public List<WebTemplateGroupkey> findGroupKeys(Integer templatePackageId,
			List<String> groupKeys) {
		if (templatePackageId == null)
			return Lists.newArrayList();
		return baseDao.find("template_package_id=" + templatePackageId
				+ " and iseff=" + IsEff.EFFECTIVE + "" + " and group_key in ("
				+ SqlUtil.array2InCondition(groupKeys) + ") order by id",
				WebTemplateGroupkey.class);
	}

	/**
	 * 根据IDS查询多个分类信息
	 * 
	 * @param groupKeyIds
	 * @return
	 * @throws DaoException
	 */
	public List<WebTemplateGroupkey> findByGroupKeyIds(List<Integer> groupKeyIds) {
		if (CollectionUtils.isEmpty(groupKeyIds))
			return Lists.newArrayList();
		String sql = " id in (" + SqlUtil.array2InCondition(groupKeyIds) + ")";
		return baseDao.find(sql, WebTemplateGroupkey.class);
	}

}
